package com.ccom.starter.rabbitmq.event;

import cn.hutool.core.util.ObjectUtil;
import com.ccom.tools.common.util.SpringContextHolder;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 监听远程事件,并分发消息到业务模块消息处理器
 */
@Component
public class BaseApplicationEventListener implements ApplicationListener<CcomRemoteApplicationEvent> {

    @Override
    public void onApplicationEvent(CcomRemoteApplicationEvent ccomRemoteApplicationEvent) {
        EventObj eventObj = ccomRemoteApplicationEvent.getEventObj();
        if (ObjectUtil.isNotEmpty(eventObj)) {
            //根据EventObj中HandlerName获取业务模块消息处理器(实现CcomBusEventHandler接口的自定义类)
            CcomBusEventHandler busEventHandler = SpringContextHolder.getHandler(eventObj.getHandlerName(), CcomBusEventHandler.class);
            if (ObjectUtil.isNotEmpty(busEventHandler)) {
                //通知业务模块
                busEventHandler.onMessage(eventObj);
            }
        }
    }
}
